﻿using System.Data;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using YiTu.DBUtility;

namespace TOUR.SZHOME.DAL
{
    public partial class LoginDAL
    {
        private const string C_SP_LOGIN_BY_NAME = "SELECT [userID],[userName],[userPassword] FROM [Login] WHERE [userName] = @userName";

        /// <summary>
        /// 根据用户名获取对象
        /// </summary>
        /// <param name="username"></param>
        /// <returns>Entity.LoginEntity实体类</returns>
        public static Entity.LoginEntity GetByName(string username)
        {
            Entity.LoginEntity entity = null;
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@userName", SqlDbType.NVarChar, 0, ParameterDirection.Input, username));

            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, C_SP_LOGIN_BY_NAME, commandParms))
            {
                if (reader.Read())
                {
                    entity = ConvertToEntityFromDataReader(reader);
                }
            }

            return entity;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newPwd"></param>
        public static bool ChangePwd(int userId, string newPwd)
        {
            List<SqlParameter> commandParms = new List<SqlParameter>();

            commandParms.Add(SqlHelper.CreateParam("@userID", SqlDbType.Int, 0, ParameterDirection.Input, userId));
            commandParms.Add(SqlHelper.CreateParam("@userPassword", SqlDbType.NVarChar, 50, ParameterDirection.Input, newPwd));
            string sql = "update dbo.Login set userPassword = @userPassword where userID = @userID";
            return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, sql, commandParms) > 0 ? true : false;
        }
    }
}
